package cn.wipace.actionlib.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;


/**
 * Created by Administrator on 2017/5/4.
 * 铁路项目的Util
 */

public class SaveUtil {
    public static final String PATH = Environment.getExternalStorageDirectory() + "/RailwayRecord";
    public static final String PROPERTIES_NAME = "config.properties";//user config store name
    public static final String JELLY_SCORE = "jelly_score";
    public static final String STACK_SCORE = "stack_score";
    public static final String ICE_SCORE = "ice_score";
    public static final String TOM_SCORE = "tom_score";



    public static void saveValueToRailwayFile(String key, String value , boolean append){
        File dir = new File(PATH);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir, PROPERTIES_NAME);
        Properties properties = new Properties();
        properties.put(key, value );
        FileOutputStream fio = null;
        try {
            fio = new FileOutputStream(file,append);
            properties.store(fio, "railway config");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fio != null) {
                try {
                    fio.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getValueToRailwayFile(String key){
        String value = "";
        File dir = new File(PATH);
        if (dir.exists()) {
            File file = new File(dir, PROPERTIES_NAME);
            if(file.exists()){
                Properties properties = new Properties();
                try {
                    FileInputStream fileInputStream = new FileInputStream(file);
                    properties.load(fileInputStream);
                    value = (String) properties.get(key);
                    if(!TextUtils.isEmpty(key)){
                        return value;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        return value;
    }



}
